//
//  HSFoodManagedViewController.m
//  hsdyOrderSystem
//
//  Created by 赖国锋 on 2017/11/8.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HSFoodManagedViewController.h"
#import "HSTypeManagedViewController.h"
#import "HSAddOrderViewController.h"
#import "HSFoodtypeModel.h"
#import "HStypeDetailVC.h"
@interface HSFoodManagedViewController ()
@property(nonatomic,strong) UIButton *addShopBtn;
@end

@implementation HSFoodManagedViewController
-(void)dealloc{
    lNOTIF_REMV();
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.addShopBtn.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.addShopBtn.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"菜品管理";
    [self setUpBarButtonItems];
    [self setupAddStopBtn];
    lNOTIF_ADD(@"foodtypeAddSuccessNotification", foodtypeAddSuccessNotification);
    lNOTIF_ADD(@"foodtypeDelSuccessNotification", foodtypeAddSuccessNotification);
    [self startPullDownRefreshing];
}

-(void)foodtypeAddSuccessNotification{
    [self loadDataSource];
}

-(void)initViews{
    [self initTableView];
    [self loadDataSource];
}

#pragma mark - Data Source
- (void)loadDefaultDataSource{
    [super loadDefaultDataSource];
    self.blankHintImage = [UIImage imageNamed:@""];
    self.blankHintString = @"暂无数据";
}

-(void)loadDataSource{
    WEAKSELF
    [XFPostRequest postShopFootTypeshopId:[BookShareUserInforDefault readUserUid] success:^(id responseObject) {
        if ([responseObject[@"status"] intValue] == 200){
            [weakSelf addToDataSource:[HSFoodtypeModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]]];
            LGFLog(@"responseObject - %@",responseObject);
        }
        else {
            if (![[HSFoodtypeModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]] isKindOfClass:[NSArray class]]) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf reloadDataWithBlank];
            }
            [weakSelf endPullDownRefreshing];
            [weakSelf setIsPageEnd:YES];
        }
        [weakSelf.tableView reloadData];
    } failure:^(NSError *error) {
        [weakSelf reloadDataWithBlank];
        [weakSelf endPullDownRefreshing];
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
        LGFLog(@"error = %@",error);
    }];
}

-(void)initTableView{
    [self initTableViewWithStyle:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.bottom.equalTo(self.view);
    }];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.sectionHeaderHeight = 40;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.1)];//消除底部空白
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.1)];//消除底部空白
}

#pragma mark - UITableView DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellInde = @"QRcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInde];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInde];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    HSFoodtypeModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.name;
    cell.tag = model.type_id ;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HStypeDetailVC *detailvc = [HStypeDetailVC new];
    detailvc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detailvc animated:YES];
}

-(void)setUpBarButtonItems{
    UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 80, 40);
//    rightBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, -15)];
    [rightBtn setTitle:@"分类管理" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = FONT(14);
    [rightBtn addTarget:self action:@selector(rightBtnItemClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)rightBtnItemClick{
    HSTypeManagedViewController *managedVC = [HSTypeManagedViewController new];
//    managedVC.dataArr = self.dataSource.copy;
    [self.navigationController pushViewController:managedVC animated:YES];
}

#pragma mark - 添加菜品
-(void)setupAddStopBtn{
    UIButton *addShopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kAppWindow addSubview:addShopBtn];
    self.addShopBtn = addShopBtn;
    addShopBtn.backgroundColor = LCOLOROFNAV;
    [addShopBtn setTitle:@"添加\n菜品" forState:UIControlStateNormal];
    addShopBtn.titleLabel.lineBreakMode = 0;
    [addShopBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addShopBtn.titleLabel.font = FONT(12);
    addShopBtn.layer.cornerRadius = SCREEN_WIDTH*0.08;
    addShopBtn.layer.shadowColor = XFUIColorFromRGB(0x666666).CGColor;//shadowColor阴影颜色
    addShopBtn.layer.shadowOffset = CGSizeMake(4,4);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    addShopBtn.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    addShopBtn.layer.shadowRadius = 4;//阴影半径，默认3
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(setPan:)];
    [addShopBtn addGestureRecognizer:pan];
    
    [addShopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(SCREEN_WIDTH*0.16);
        make.right.bottom.offset(-20);
        make.bottom.offset(-50);
    }];
    WEAKSELF
    [addShopBtn addTouches:^{
        HSAddOrderViewController *managedVC = [HSAddOrderViewController new];
        [weakSelf.navigationController pushViewController:managedVC animated:YES];
    }];
}

//拖动
-(void)setPan:(UIPanGestureRecognizer*)sender{
    CGPoint point = [sender translationInView:self.view];
    CGFloat height =sender.view.height*0.5;
    if (sender.view.center.x < height) {
        sender.view.center = CGPointMake(height, sender.view.center.y + point.y);
    }else if (sender.view.center.x > self.view.frame.size.width - height) {
        sender.view.center = CGPointMake(self.view.frame.size.width - height, sender.view.center.y + point.y);
    }else if (sender.view.center.y < height+64) {
        sender.view.center = CGPointMake(sender.view.center.x + point.x, height+64);
    }else if (sender.view.center.y > SCREEN_HEIGHT-height){
        sender.view.center = CGPointMake(sender.view.center.x + point.x, SCREEN_HEIGHT-height);
    }else{
        sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y);
    }
    [sender setTranslation:CGPointZero inView:self.view];
}
@end
